package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.Constants;
import com.foreveross.common.SQLUtil;
import com.foreveross.dao.HrOptimizedDao;
import com.foreveross.vo.HrOptimized;

@Component("hrOptimizedDao")
public class HrOptimizedDaoImp extends SQLUtil implements HrOptimizedDao {

	

	@Override
	public long getHrOptimizedListCount(String weixinID, String userId,
			String title, String houseId) throws Exception {
		StringBuffer strbuBuffer = new StringBuffer();
		strbuBuffer.append(" select count(*) from HrOptimized model where 1=1 ");
		/*if(!"".equals(weixinID) && null != weixinID){
			strbuBuffer.append(" and (model.houseId = '' or model.houseId is null or model.houseId in (select b.hrHouses from HouseUser b where wechatId= '"+weixinID+"'))");
		}*/
		if(!"".equals(weixinID) && null != weixinID){
			strbuBuffer.append(" and model.id in(select optimized from OptimizedHouse where house in ");
			strbuBuffer.append("(select b.hrHouses from HouseUser b where b.authenticationStatus = '"+Constants.AUTHENTICATIONSTATUS_SUCCESS+"'");
			strbuBuffer.append(" and b.wechatId= '"+weixinID+"'))");
//			strbuBuffer.append(" and model.id in(select optimized from OptimizedHouse where house in ");
//			strbuBuffer.append("(select b.hrHouses from HouseUser b where wechatId= '"+weixinID+"'))");
			
		}
		if(title!=null && !"".equals(title)){
			strbuBuffer.append(" and  model.title like '%"+title+"%'");
		}
		if(houseId!=null && !"".equals(houseId)){
			//strbuBuffer.append(" and  model.houseId = '"+houseId+"'");
			strbuBuffer.append(" and model in (select optimized from OptimizedHouse optimizedHouse where optimizedHouse.house ='" + houseId + "')");
		}
		if(userId!=null && !"".equals(userId)){
			strbuBuffer.append(" and  model in(select oh.optimized from OptimizedHouse oh where oh.house"
					+ " in (select rh.hrHouses from SysRoleHouse rh where rh.sysRole in (select ur.sysRole from  SysUserRole ur where ur.sysUser.id='"
					+ userId + "')))");
			//this.addAuthorityStr(strbuBuffer, userId,  "houseId");
//			strbuBuffer.append(" and  model.createId = '"+userId+"'");
		}
		strbuBuffer.append(" or  model.isAll ='"+ Constants.IS_ALL + "'");
		return Long.parseLong(findObejctByHql(strbuBuffer.toString()).toString());
	}

	@Override
	public List<HrOptimized> getHrOptimizedList(String weixinID, String userId,
			String title, String houseId, int pageIndex, int pageSize) throws Exception  {
		StringBuffer strbuBuffer = new StringBuffer();
		strbuBuffer.append(" select model from HrOptimized model where 1=1 ");
		/*if(!"".equals(weixinID) && null != weixinID){
			strbuBuffer.append(" and (model.houseId = '' or model.houseId is null or model.houseId in (select b.hrHouses from HouseUser b where wechatId= '"+weixinID+"'))");
		}*/
		if(!"".equals(weixinID) && null != weixinID){
			strbuBuffer.append(" and model.id in(select optimized from OptimizedHouse where house in ");
			strbuBuffer.append("(select b.hrHouses from HouseUser b where b.authenticationStatus = '"+Constants.AUTHENTICATIONSTATUS_SUCCESS+"'");
			strbuBuffer.append(" and b.wechatId= '"+weixinID+"'))");
		}
		if(title!=null && !"".equals(title)){
			strbuBuffer.append(" and  model.title like '%"+title+"%'");
		}
		if(houseId!=null && !"".equals(houseId)){
			//strbuBuffer.append(" and  model.houseId = '"+houseId+"'");
			strbuBuffer.append(" and model in (select optimized from OptimizedHouse optimizedHouse where optimizedHouse.house ='" + houseId + "')");
		}
		if(userId!=null && !"".equals(userId)){
			strbuBuffer.append(" and  model in(select oh.optimized from OptimizedHouse oh where oh.house"
					+ " in (select rh.hrHouses from SysRoleHouse rh where rh.sysRole in (select ur.sysRole from  SysUserRole ur where ur.sysUser.id='"
					+ userId + "')))");
//			this.addAuthorityStr(strbuBuffer, userId,  "houseId");
//			strbuBuffer.append(" and  model.createId = '"+userId+"'");
		}
		strbuBuffer.append(" or model.isAll ='"+ Constants.IS_ALL + "'");
		strbuBuffer.append(" order by model.createTime desc");
		return (List<HrOptimized>) findObjectAll(strbuBuffer.toString(), pageIndex,pageSize);
	}

}
